package com.project.demo.controller;

import com.aliyun.imagerecog20190930.models.ClassifyingRubbishResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;
import com.project.demo.controller.base.BaseController;
import com.project.demo.entity.Rubbish;
import com.project.demo.service.RubbishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;

/**
 * 垃圾识别
 *
 */
@RestController
@RequestMapping("rubbish")
public class RubbishController extends BaseController<Rubbish, RubbishService> {
    /**
     * 服务对象
     */
    @Autowired
    public RubbishController(RubbishService service) {
        setService(service);
    }

    /**
     * 垃圾识别
     * @param request
     * @return
     */
    @GetMapping("search_rubbish")
    public Map<String, Object> quit(HttpServletRequest request) {
        Map<String,Object> map = new HashMap<>();
        Map<String,String> query =  service.readQuery(request);
        if (!StringUtils.isEmpty(query.get("img_url"))){
            try {
                String accessKeyId = "LTAI5tGoXpdVrgB3PbV3yWTk";
                String accessKeySecret = "8p9GXk53fy1dnz0fapJtVSRU8KoeQ0";
                com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                        .setAccessKeyId(accessKeyId)
                        .setAccessKeySecret(accessKeySecret);
                // 访问的域名
                config.endpoint = "imagerecog.cn-shanghai.aliyuncs.com";
                com.aliyun.imagerecog20190930.Client client = new com.aliyun.imagerecog20190930.Client(config);
                InputStream inputStream = Files.newInputStream(new File(System.getProperty("user.dir") + "/src/main/resources/static/"+query.get("img_url")).toPath());

                com.aliyun.imagerecog20190930.models.ClassifyingRubbishAdvanceRequest classifyingRubbishAdvanceRequest = new com.aliyun.imagerecog20190930.models.ClassifyingRubbishAdvanceRequest()
                        .setImageURLObject(inputStream);
                com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
                try {
                    ClassifyingRubbishResponse classifyingRubbishAdvanceResponse = client.classifyingRubbishAdvance(classifyingRubbishAdvanceRequest, runtime);
                    map.put("code",200);
                    map.put("msg","成功");
                    map.put("data",classifyingRubbishAdvanceResponse.getBody().getData().elements.get(0));
                    Map<String,Object> body = new HashMap<>();
                    body.put("img_url","/api/upload/"+query.get("img_url"));
                    body.put("title",classifyingRubbishAdvanceResponse.getBody().getData().elements.get(0).getRubbish());
                    body.put("type",classifyingRubbishAdvanceResponse.getBody().getData().elements.get(0).getCategory());
                    body.put("user_id",query.get("user_id"));
                    service.insert(body);
                } catch (TeaException teaException) {
                    map.put("code",500);
                    map.put("msg","识别失败");
                    map.put("data",new HashMap<>());
                    teaException.printStackTrace();
                }
            }catch (Exception e){
                map.put("code",500);
                map.put("msg","请上传图片");
                map.put("data",new HashMap<>());
                e.printStackTrace();
            }
        }
        return map;
    }

}


